package config

import "fmt"

type ClusterType uint8

const (
	CTMaster ClusterType = iota
	CTSlave
)

type mysqlConf struct {
	Addr      []string
	DB        string
	User      string
	Pass      string
	MaxIdle   int
	MaxActive int
	Ct        ClusterType
}

type redisConf struct {
	Host       string
	Port       uint16
	User       string
	Pass       string
	hasRecover bool
}

// Conf 配置结构体
type Conf struct {
	ListenPort uint16 `yaml:"listen"`
	Mysql      []*mysqlConf
	Redis      *redisConf
}

// GetRedisURI 获取redis uri
func (c *redisConf) GetURI() string {
	return fmt.Sprintf("%s:%d", c.Host, c.Port)
}

var instance *Conf

// Instance 获取配置单例
func Instance() *Conf {
	if instance == nil {
		instance = &Conf{}
	}
	return instance
}

// GetMysqURI 获取Mysql uri
func (c *mysqlConf) GetURI() []string {
	mAddr := make([]string, 0, len(c.Addr))
	for _, addr := range c.Addr {
		mAddr = append(mAddr, fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8&loc=Local&parseTime=true",
			c.User, c.Pass, addr, c.DB))
	}
	return mAddr
}
